'use strict';

const Controller = require('egg').Controller;

class ReportController extends Controller {
  async save() {
    let currentuserid = this.ctx.authUser.id;
    this.ctx.validate({
      reported_id: {
        type: "int",
        required: true,
        defValue: "",
        desc: "被举报人/群组id",
      },
      reported_type: {
        type: "string",
        required: true,
        range: {
          in: ["user", "group"]
        },

        desc: "举报类型",
      },
      content: {
        type: "string",
        required: true,

        desc: "举报内容",
      },
      category: {
        type: "string",
        required: true,

        desc: "举报内容",
      },
    });
    if (this.ctx.request.body.reported_type === 'user' && currentuserid === this.ctx.request.body.reported_id) {
      this.ctx.throw(400, "不能举报自己")
    }
    let user = await this.app.model.User.findOne({
      where:{
        id: this.ctx.request.body.reported_id,
        status: 1
      }

    })

    if (!user) {
      this.ctx.throw(400, "该用户不存在")
    }
    let isreport=await this.app.model.Report.findOne({
      where:{
        reported_id:this.ctx.request.body.reported_id,
        reported_type:this.ctx.request.body.reported_type,
        status:"pending"
      }
    })
    console.log(isreport,"is")
    if(isreport){
      this.ctx.throw(400,"不允许反复提交")
    }
    let res=await this.app.model.Report.create({
      user_id:currentuserid,
      reported_id:this.ctx.request.body.reported_id,
      reported_type:this.ctx.request.body.reported_type ,
      content: this.ctx.request.body.content, 
      category: this.ctx.request.body.category, 
    })

this.ctx.apiSuccess(res,"举报成功",200)
  }
}

module.exports = ReportController;
